<template>
  <div>
    <!-- 在搜索框下方添加清空按钮 -->
    <div style="margin-bottom: 20px; display: flex; gap: 10px">
      <el-input
        v-model="searchKeyword"
        placeholder="请输入订单号、书籍名称等关键字搜索"
        style="width: 300px;"
      >
        <template #append>
          <el-button @click="handleSearch">搜索</el-button>
        </template>
      </el-input>
      <el-button type="danger" @click="clearCart">清空购物车</el-button>
    </div>

    <el-table
      :data="filteredTableData"
      style="width: 100%">

        <el-table-column
        prop="picture"
        label="书籍封面"
        width="300">
          <template width="90" slot-scope="scope">
             <img style="width:250px;height:250px;border:none;" :src="scope.row.bookimg">
          </template>
        </el-table-column>

        <el-table-column
          label="订单号"
          width="180">
          <template slot-scope="scope">
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.code }}</el-tag>
              </div>
          </template>
        </el-table-column>

        <el-table-column
        label="书籍名称"
        width="220">
        <template slot-scope="scope">
          <i class="is-text"></i>
          <span style="margin-left: 50px">{{ scope.row.bookname }}</span>
        </template>
        </el-table-column>

        <el-table-column
          label="购买用户"
          width="200">
          <template slot-scope="scope">
            <i class="is-text"></i>
            <span style="margin-left: 20px">{{ scope.row.username }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="创建时间"
          width="200">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.createtime }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="距离创建时间"
          width="200">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.difftime }} min </span>
          </template>
        </el-table-column>

        <el-table-column
        label="价格"
        width="180">
        <template slot-scope="scope">
          <i class="is-text"></i>
          <span style="margin-left: 10px">￥{{ scope.row.price }} </span>
        </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <div style="display: flex; gap: 8px; align-items: center">
              <el-button
                size="mini"
                type="success"
                @click="handlePay(scope.row)">立即支付</el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row.code)">删除</el-button>
              <el-input-number 
                v-model="scope.row.quantity"
                :min="1"
                :max="10"
                @change="handleQuantityChange(scope.row)"
                size="mini"></el-input-number>
            </div>
          </template>
        </el-table-column>

      </el-table>
  </div>
</template>

<script>
  export default {
    name: 'ShoppingCart',
    data() {
      return {
        openOff1: false,
        tableData: [],
        searchKeyword: '',
        filteredTableData: []
      }
    },
    created() {
      this.loadCartData();
    },
    methods: {
      loadCartData() {
        const cartData = localStorage.getItem('cart');
        if (cartData) {
          this.tableData = JSON.parse(cartData);
          this.filteredTableData = [...this.tableData];
        }
      },
      handlePay(row) {
        console.log("结算如下");
        console.log(row.bookimg);
        console.log(row.code);
        console.log(row.bookname);
        console.log(row.username);
        console.log(row.createtime);
        console.log(row.difftime);
        console.log(row.price);
        let code = row.code;
        // 模拟支付请求
        this.openOff1 = true;  // 打开提示
        const loading = this.$loading({
          lock: true,
          text: '10s后跳转到商品页面~',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        setTimeout(() => {
          this.$router.push({path: '/BookList'}); // 强制切换到商品页面
          loading.close();
        }, 10000);
      },
      handleDelete(orderCode) {
        this.$confirm('确认删除该商品吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableData = this.tableData.filter(item => item.code !== orderCode);
          localStorage.setItem('cart', JSON.stringify(this.tableData));
          this.$message.success('删除成功');
        }).catch(() => {});
      },
      handleQuantityChange(item) {
        localStorage.setItem('cart', JSON.stringify(this.tableData));
        this.$message.success('数量更新成功');
      },
      handleSearch() {
        if (this.searchKeyword) {
          this.filteredTableData = this.tableData.filter(item => {
            return item.code.includes(this.searchKeyword) || item.bookname.includes(this.searchKeyword);
          });
        } else {
          this.filteredTableData = this.tableData;
        }
      },
      clearCart() {
        this.$confirm('确认清空购物车吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          localStorage.removeItem('cart');
          this.tableData = [];
          this.filteredTableData = [];
          this.$message.success('购物车已清空');
        }).catch(() => {});
      }
    }
  }
</script>

<style scoped>
.el-table {
  margin: 20px 0;
}
.el-table__row td {
  padding: 12px 0;
  vertical-align: middle;
}
</style>
